<?php
/**
 * Created by PhpStorm.
 * User: Tangyijun
 * Date: 2017/4/1
 * Time: 10:58
 */
namespace app\wechat\controller;
use app\home\controller\Integral;
use app\models\Bases;
use app\service\Wechat;
use think\Config;

class Index extends Base{
    /**
     * 微信按钮跳转授权
     */
    public function weChatAccredit(){
        $url = 'http://'.$_SERVER['HTTP_HOST'].'/wechat/index/getChatInfo';
        $we_chat = new Wechat();
        $we_chat->accredit($url);
    }

    /**
     * 微信支付授权
     */
    public function payAccredit(){
        $url = 'http://'.$_SERVER['HTTP_HOST'].'/wechat/index/activity';
        $we_chat = new Wechat();
        $we_chat->accredit($url);
    }

    /**
     * 获取微信用户信息
     */
    public function getChatInfo(){
        $we_chat = new Wechat();
        $code = $_GET['code'];
        $user = new Bases('user');
        $access_token = $we_chat->getAccessToken($code);
        $we_chat_user_info = $we_chat->getWeChatUserInfo($access_token['access_token'],$access_token['openid']);
        $user_info = $user->find("openid = '{$access_token['openid']}'");
        $param = ['first_login' => Config::get('wechat.is_not_first_login'),'openid' => $we_chat_user_info['openid']];
        cookie('openid',$we_chat_user_info['openid']);
        if(empty($user_info)){
            $data = [
                'nickname' => $we_chat_user_info['nickname'],
                'openid'   => $we_chat_user_info['openid'],
                'avatar'   => $we_chat_user_info['headimgurl'],
                'source'   => 2,
                'integral' => 50,
                'is_remind' => 2,
                'create_time' => time(),
            ];
            if($user->insertData($data)){
                $param = ['first_login' => Config::get('wechat.is_first_login')];
            }
        }
        $this->redirect(url('index/index',$param));
    }
    /**
     * @return \think\response\View
     */
    public function index(){
        $param =  input('param.');
        $openid = cookie('openid');
        $user_info = self::getUserInfo("openid = '{$openid}'",'nickname,openid,avatar,validity,username,uid,integral');
        if(!empty($user_info['openid']) && !empty($user_info['username'])){
            cookie('uid',$user_info['uid'],time()+3600*24*30);
            //查看用户是否扣除过积分
            $integral = new Integral();
            $is_deduction_integral = $integral->checkDeductionIntegral();
            $this->assign('is_deduction_integral',$is_deduction_integral);
        }
        return view('index',['param'=>$param,'user_info'=>$user_info]);
    }

    /**
     * @return \think\response\Json
     * 验证用户是否扣除积分
     */
    public function checkDedInt(){
        try{
            $openid = cookie('openid');
            $user_info = self::getUserInfo("openid = '{$openid}'",'openid,username');
            if(!empty($user_info['openid']) && !empty($user_info['username'])){
                $integral = new Integral();
                $is_deduction_integral = $integral->checkDeductionIntegral();
                return json(self::formatSuccessResult(['is_deduction_integral' => $is_deduction_integral]));
            }else{
                throw new \LogicException('请先绑定用户',10020);
            }
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
    }

    /**
     * @return \think\response\View
     * 产品介绍
     */
    public function activity(){
        $we_chat = new Wechat();
        $user = new Bases('user');
        $code = empty($_GET['code']) ? 0 : $_GET['code'];
        $access_token = $we_chat->getAccessToken($code);
        $user_info = $user->find("openid = '{$access_token['openid']}'");
        $we_chat_user_info = $we_chat->getWeChatUserInfo($access_token['access_token'],$access_token['openid']); //获取微信用户信息
        if(empty($user_info)){
            $data = [
                'nickname' => $we_chat_user_info['nickname'],
                'openid'   => $we_chat_user_info['openid'],
                'avatar'   => $we_chat_user_info['headimgurl'],
                'source'   => 2,
                'integral' => 50,
                'is_remind' => 2,
                'create_time' => time(),
            ];
            if($user->insertData($data)){
                $param = ['first_login' => Config::get('wechat.is_first_login')];
            }
        }
        cookie('openid',$access_token['openid'],3600*24*7 + time());
        return view('activity');
    }
}